﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DevExpress.Web.ASPxGridView;
using Trangtrai.Controller.HistoryController;
using Trangtrai.Model;
using TrangTrai.Data.DAO;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxClasses;
namespace Trangtrai.nhatky
{
    public partial class TomHis : System.Web.UI.Page
    {
        HisDAOManagement hisMana = new HisDAOManagement();
        HisInfo[] hisInfo = null;
        CuanHisStatus[] cuanHis = null;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !IsCallback)
            {
                autoGenColumn();
                bindingData();
            }

        }
        public CuanHisStatus[] removeObjectFromHis(CuanHisStatus[] a, CuanHisStatus b)
        {
            int index = 0;
            CuanHisStatus[] temp = new CuanHisStatus[a.Length - 1];
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i].choanID != b.choanID)
                {
                    temp[index] = a[i];
                    index++;
                }
            }
            return temp;
        }
        public void bindingData()
        {
            int index = 1;
            int indexMask = 0;
            int countMask = 0;

            hisInfo = hisMana.getHis();
            cuanHis = hisMana.getCuanHis();
            if (cuanHis != null && cuanHis.Length > 0)
            {
                for (int i = 0; i < hisInfo.Length; i++)
                {
                    int choAnID = cuanHis[indexMask].choanID;
                    for (int j = indexMask; j < cuanHis.Length; j++)
                    {
                        CuanHisStatus cuanH = cuanHis[j];
                        if (cuanH.choanID == choAnID)
                        {
                            switch (index)
                            {
                                case 1:
                                    hisInfo[i].cuanStatus1 = cuanH.statusName;
                                    hisInfo[i].cuanAmount1 = cuanH.actualAmount;
                                    break;
                                case 2:
                                    hisInfo[i].cuanStatus2 = cuanH.statusName;
                                    hisInfo[i].cuanAmount2 = cuanH.actualAmount;
                                    break;
                                case 3:
                                    hisInfo[i].cuanStatus3 = cuanH.statusName;
                                    hisInfo[i].cuanAmount3 = cuanH.actualAmount;
                                    break;
                                case 4:
                                    hisInfo[i].cuanStatus4 = cuanH.statusName;
                                    hisInfo[i].cuanAmount4 = cuanH.actualAmount;
                                    break;
                                case 5:
                                    hisInfo[i].cuanStatus5 = cuanH.statusName;
                                    hisInfo[i].cuanAmount5 = cuanH.actualAmount;
                                    break;
                            }
                        }
                        else if (countMask == 0)
                        {
                            indexMask = j;
                            countMask++;
                        }
                        index++;
                    }
                    index = 1;
                    countMask = 0;
                }
            }
            tomHis.DataSource = hisInfo;
            tomHis.DataBind();
        }
        public void autoGenColumn()
        {
            int index = 1;
            List<Cuan>  cuanList = TomHisController.getCua();
            if (cuanList != null)
            {
                GridViewBandColumn bandCol = new GridViewBandColumn();
                GridViewBandColumn foodCol = new GridViewBandColumn();
                foreach (Cuan cuan in cuanList)
                {
                    GridViewDataComboBoxColumn cuanData = new GridViewDataComboBoxColumn();
                    GridViewDataTextColumn foodData = new GridViewDataTextColumn();

                    cuanData.FieldName = "cuanStatus" + index;
                    cuanData.Caption = "Lần " + index + "(" + cuan.time_cuan + "h)";
                    cuanData.PropertiesComboBox.ValueField = "cuanStatus";
                    cuanData.PropertiesComboBox.TextField = "cuanStatus";

                    foodData.FieldName = "cuanAmount" + index;
                    foodData.Caption = cuan.time_cuan + "h";

                    bandCol.Columns.Add(cuanData);
                    foodCol.Columns.Add(foodData);
                    
                    index++;
                }
                bandCol.Caption = "Kiểm tra vó";
                bandCol.VisibleIndex = 2;
                bandCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                this.tomHis.Columns.Add(bandCol);

                foodCol.Caption = "Thức ăn (KG)";
                foodCol.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                foodCol.VisibleIndex = 3;
                this.tomHis.Columns.Add(foodCol);

            }
        }

        protected void tomHis_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
        {
            if (!tomHis.IsEditing) return;
            else if (e.Column.FieldName == "aoName")
            {
                ASPxComboBox combo = e.Editor as ASPxComboBox;
                FillData(combo, e.Column.FieldName);
                combo.Callback += new CallbackEventHandlerBase(cmbAo_OnCallback);
            }
            else if (e.Column.FieldName == "foodCode")
            {
                ASPxComboBox combo = e.Editor as ASPxComboBox;
                FillData(combo, e.Column.FieldName);
                combo.Callback += new CallbackEventHandlerBase(cmbFood_OnCallback);
            }
            else if (e.Editor is ASPxComboBox)
            {
                ASPxComboBox combo = e.Editor as ASPxComboBox;
                FillData(combo, "cuanStatus");
                combo.Callback += new CallbackEventHandlerBase(cmb_statusCallback);
            }
        }
        protected void FillData(ASPxComboBox cmb, string fileName)
        {
            if (fileName.Equals("aoName", StringComparison.OrdinalIgnoreCase))
            {
                AoHis[] aoHis = hisMana.getAoHis();
                cmb.DataSource = aoHis;
                cmb.DataBind();
            }
            if (fileName.Equals("cuanStatus", StringComparison.OrdinalIgnoreCase))
            {
                Status[] status = hisMana.getStatus();
                cmb.DataSource = status;
                cmb.DataBind();
            }
            if (fileName.Equals("foodCode", StringComparison.OrdinalIgnoreCase))
            {
                FoodCodeInfo[] foodCodeInfo = hisMana.getFoodCode();
                cmb.DataSource = foodCodeInfo;
                cmb.DataBind();
            }
        }
        private void cmbAo_OnCallback(object source, CallbackEventArgsBase e)
        {
            FillData(source as ASPxComboBox, "aoName");
        }
        private void cmbFood_OnCallback(object source, CallbackEventArgsBase e)
        {
            FillData(source as ASPxComboBox, "foodCode");
        }
        private void cmb_statusCallback(object source, CallbackEventArgsBase e)
        {
            FillData(source as ASPxComboBox, "cuanStatus");
        }

        protected void tomHis_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
            //String dateTime = time.Day + time.Month + time.Year;
            TomHisController.insert2Log(e.NewValues["aoName"].ToString(), Int32.Parse(e.NewValues["age"].ToString()),
                e.NewValues["note"].ToString(), e.NewValues["foodCode"].ToString(), "admin");
            TomHisController.insertIntoInfo(Double.Parse(e.NewValues["doKiem"].ToString()),Double.Parse(e.NewValues["doTrong"].ToString()),
                Double.Parse(e.NewValues["doMan"].ToString()),e.NewValues["mau"].ToString(),e.NewValues["hienTuong"].ToString(),
                Double.Parse(e.NewValues["bod"].ToString()),Double.Parse(e.NewValues["cod"].ToString()),Double.Parse(e.NewValues["hoaTan"].ToString()),
                e.NewValues["tomStatus"].ToString(),Int32.Parse(e.NewValues["tomSize"].ToString()),Double.Parse(e.NewValues["phSang"].ToString()),
                Double.Parse(e.NewValues["phChieu"].ToString()));
            
            int index = 1;
            List<Cuan> listCA = TomHisController.getCua();
            if (listCA != null)
            {
                foreach (Cuan cuan in listCA)
                {
                    TomHisController.insertToCuAnHis(e.NewValues["cuanStatus" + index].ToString(), Int32.Parse(e.NewValues["cuanAmount" + index].ToString()),
                        cuan.cuan_id);
                    index++;
                }
            }
            bindingData();
            e.Cancel = true;
        }

        protected void tomHis_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
        {
            TomHisController.deleteLogInfo(Int32.Parse(e.Keys[0].ToString()));
            bindingData();
            e.Cancel = true;
        }

        protected void createReport_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/nhatky/NormalReport.aspx");
        }
    }
}
